package com.shiliao.app.activity.index.ui;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.SwipeRefreshLayout;

import com.shiliao.app.R;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.MyWebView;
import com.shiliao.app.utils.WebSetUtil;
import com.shiliao.app.utils.YMServerUtil;
import com.shiliao.app.weight.ProgressWebView;
import com.yimei.framework.base.ui.BaseFragment;
import com.yimei.framework.util.LogUtil;

import butterknife.BindView;

/**
 * Created by yinzhiqiang on 2017/9/7.
 */

public class MyFavoritesFragment extends BaseFragment {
    @BindView(R.id.webview)
    ProgressWebView webview;
    @BindView(R.id.swip_refresh)
    SwipeRefreshLayout refresh;

    private MyWebView mWebView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_daily_knowledge;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initFragmentView() {
        super.initFragmentView();
        WebSetUtil.setWebView(webview);
        intedata();
        String userId = YMUserManager.getUserId(getContext());
        final String url = YMServerUtil.getServerUrl() + YMServerUtil.ACTION_MYARTICLE + "?user_id=" + userId;
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LogUtil.e("yzq", url);
                webview.loadUrl(url);
            }
        });
        LogUtil.e("yzq", "..." + url);
        webview.loadUrl(url);
    }

    private void intedata() {
        Intent intent = new Intent(getContext(), NewsDetailActivity.class);
        mWebView = new MyWebView(webview, getContext(), YMServerUtil.MY_FAVORITES_COTURL,
                intent, refresh, YMServerUtil.MY_FAVORITES_REFERASHURL);
        webview.setWebViewClient(mWebView);
    }

    private boolean isVisible;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtil.e("yzq", ".....................");
        if (getUserVisibleHint()) {
            isVisible = true;
            webview.reload();
        } else {
            isVisible = false;
        }

    }

}
